<?
function microtime_float() {
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}

class _database {
	private static $connected = false;
	private static $link = null;

	public function  __construct() {
		return true;
	}

	private static function connect() {

		if(self::$connected) return self::$instance;
		$link = mysql_connect(conf::$db[0],conf::$db[1],conf::$db[2]);
		$sel = mysql_select_db(conf::$db[3],$link);
		if(!$sel) {
			die('cant select database');
		}
		else {
			mysql_query('SET NAMES UTF8',$link);
			self::$connected = true;
			self::$link = $link;
			return true;
		}
	}

	public static function query($query,$nosave=false) {

		if(!self::$connected) self::connect();
		$start = microtime_float();
		$r = mysql_query($query);
		$execution_time = microtime_float() - $start;
		if(!$r) {
			return false;
		}
		return $r;
	}

	public static function torow($query,$m = MYSQL_ASSOC) {
		$r = self::query($query);
		$tmp = array();
		$row = mysql_fetch_array($r , $m);
		return $row?$row:false;
	}

	public static function toarray($query,$key = false,$m = MYSQL_ASSOC) {
		$r = self::query($query);
		$tmp = array();
		if($key) {
			while($row = mysql_fetch_array($r , $m)) {
				$tmp[$row[$key]]  = $row;
			}
		}
		else {
			while($row = mysql_fetch_array($r , $m)) {
				$tmp[]  = $row;
			}
		}
		return $tmp;
	}
}
?>
